﻿
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace Admin.NET.Core.Fitter
{
    /// <summary>
    /// 并发过滤器
    /// </summary>

    public class OccurSimultaneouslyAttribute : Attribute, IFilterFactory
    {
        private readonly string[] _factorNames;
        private readonly double _expiredSeconds;

        public OccurSimultaneouslyAttribute(double expiredSeconds, params string[] factorNames)
        {
            _expiredSeconds = expiredSeconds;
            _factorNames = factorNames;
        }

        public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
        {
            //注册 OccurSimultaneouslyActionFilter 服务
            var filter = serviceProvider.GetService<OccurSimultaneouslyActionFilter>();
            //2个入参 方法名和时间(秒)
            filter.FactorNames = _factorNames;
            filter.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(_expiredSeconds);
            return filter;
        }
        public bool IsReusable => false;


    }
}
